package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.GwatT;
import cn.e4j.modular.crm.entity.GwatlT;
import cn.e4j.modular.crm.mapper.GwatTMapper;
import cn.e4j.modular.crm.service.GwatTService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  配件仓库库位设置
 *  服务实现类
 * </p>
 *
 * @author mengyb
 * @since 2019-04-18
 */
@Service
public class GwatTServiceImpl extends ServiceImpl<GwatTMapper,GwatT> implements GwatTService {
    @Transactional(readOnly = true)
    @Override
    public List<Map<String, Object>> getList(Dto dto, Page<Object> page) {

        ShiroUser shiroUser = ShiroKit.getUser();

        dto.put("ent", shiroUser.getDBC());
        dto.put("lang", LangUtils.getLang(shiroUser.getLANG()));

        return this.baseMapper.getList(dto, page);

    }
    @Transactional(readOnly = true)
    @Override
    public List<Map<String, Object>> getGwasPageList(Dto dto, Page<Object> page) {
        ShiroUser shiroUser = ShiroKit.getUser();

        dto.put("ent", shiroUser.getDBC());
        dto.put("lang", LangUtils.getLang(shiroUser.getLANG()));

        return this.baseMapper.getGwasList(dto, page);
    }
    @Transactional(readOnly = true)
    @Override
    public List<Map<String, Object>> getGwasList(Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();

        dto.put("ent", shiroUser.getDBC());
        dto.put("lang", LangUtils.getLang(shiroUser.getLANG()));

        return this.baseMapper.getGwasList(dto);
    }

    @Transactional
    @Override
    public void add(GwatT gwatT, GwatlT gwatlT) {

        ShiroUser shiroUser = ShiroKit.getUser();

        gwatT.setGWATENT(shiroUser.getDBC());
        gwatT.setGWATDATE(LocalDate.now());
        gwatT.setGWATTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
        gwatT.setGWATUSER(shiroUser.getACCOUNT());
        gwatT.setGWATGRUP(shiroUser.getORG_ID());
        gwatT.setGWATORIU(shiroUser.getACCOUNT());
        gwatT.setGWATORIG(shiroUser.getORG_ID());

        gwatlT.setGWATLENT(shiroUser.getDBC());
        gwatlT.setGWATL001(gwatT.getGWAT001());
        gwatlT.setGWATL002(gwatT.getGWAT002());
        gwatlT.setGWATL003(LangUtils.getLang(shiroUser.getLANG()));

        gwatT.insert();
        gwatlT.insert();
    }

    @Transactional
    @Override
    public void edit(GwatT gwatT, GwatlT gwatlT) {

        ShiroUser shiroUser = ShiroKit.getUser();

        gwatT.setGWATMODU(shiroUser.getACCOUNT());

        UpdateWrapper<GwatT> gwatTUpdateWrapper = new UpdateWrapper<>();
        gwatTUpdateWrapper.eq("GWATENT", gwatT.getGWATENT());
        gwatTUpdateWrapper.eq("GWAT001", gwatT.getGWAT001());
        gwatTUpdateWrapper.eq("GWAT002", gwatT.getGWAT002());

        UpdateWrapper<GwatlT> gwatlTUpdateWrapper = new UpdateWrapper<>();
        gwatlTUpdateWrapper.eq("GWATLENT", gwatT.getGWATENT());
        gwatlTUpdateWrapper.eq("GWATL001", gwatT.getGWAT001());
        gwatlTUpdateWrapper.eq("GWATL002", gwatT.getGWAT002());


        gwatT.update(gwatTUpdateWrapper);
        gwatlT.update(gwatlTUpdateWrapper);

    }

    @Transactional
    @Override
    public void delRow(GwatT gwatT, GwatlT gwatlT) {

        UpdateWrapper<GwatT> gwatTUpdateWrapper = new UpdateWrapper<>();
        gwatTUpdateWrapper.eq("GWATENT", gwatT.getGWATENT());
        gwatTUpdateWrapper.eq("GWAT001", gwatT.getGWAT001());
        gwatTUpdateWrapper.eq("GWAT002", gwatT.getGWAT002());

        UpdateWrapper<GwatlT> gwatlTUpdateWrapper = new UpdateWrapper<>();
        gwatlTUpdateWrapper.eq("GWATLENT", gwatT.getGWATENT());
        gwatlTUpdateWrapper.eq("GWATL001", gwatT.getGWAT001());
        gwatlTUpdateWrapper.eq("GWATL002", gwatT.getGWAT002());


        gwatT.delete(gwatTUpdateWrapper);
        gwatlT.delete(gwatlTUpdateWrapper);

    }

    @Transactional
    @Override
    public void delRows(List<GwatT> gwatTList) {
        for (GwatT gwatT : gwatTList) {
            UpdateWrapper<GwatT> gwatTUpdateWrapper = new UpdateWrapper<>();
            gwatTUpdateWrapper.eq("GWATENT", gwatT.getGWATENT());
            gwatTUpdateWrapper.eq("GWAT001", gwatT.getGWAT001());
            gwatTUpdateWrapper.eq("GWAT002", gwatT.getGWAT002());

            GwatlT gwatlT=new GwatlT();
            UpdateWrapper<GwatlT> gwatlTUpdateWrapper = new UpdateWrapper<>();
            gwatlTUpdateWrapper.eq("GWATLENT", gwatT.getGWATENT());
            gwatlTUpdateWrapper.eq("GWATL001", gwatT.getGWAT001());
            gwatlTUpdateWrapper.eq("GWATL002", gwatT.getGWAT002());


            gwatT.delete(gwatTUpdateWrapper);
            gwatlT.delete(gwatlTUpdateWrapper);
        }
    }

    /**
     * 查重
     * @param gwatT
     * @return
     */
    @Transactional(readOnly = true)
    @Override
    public Boolean existedTheParams(GwatT gwatT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        QueryWrapper<GwatT> gwatTQueryWrapper = new QueryWrapper<>();
        gwatTQueryWrapper.eq("GWATENT", shiroUser.getDBC());
        gwatTQueryWrapper.eq("GWAT001", gwatT.getGWAT001());
        gwatTQueryWrapper.eq("GWAT002", gwatT.getGWAT002());
        return this.baseMapper.selectList(gwatTQueryWrapper).size() > 0;
    }
}
